package com.qcnh.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qcnh.entity.Announcement;
import com.qcnh.entity.Payment;
import com.qcnh.entity.User;
import com.qcnh.service.impl.AnnouncementServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import unil.Result;

import java.util.List;

/**
 * <p>
 * 公告表 前端控制器
 * </p>
 */
@RestController
@RequestMapping("announcement")
public class AnnouncementController {
    @Autowired
    private AnnouncementServiceImpl announcementService;

    // 分页查询公告并支持根据标题模糊查询
    @GetMapping("selectAllAnnouncements")
    public Result selectAllAnnouncements(@RequestParam(value = "current",defaultValue = "1") Integer current,
                            @RequestParam(value = "size",defaultValue = "10") Integer size,
                            @RequestParam(value = "keyWord",defaultValue = "false") String keyWord) {
        IPage<Announcement> Ipage = announcementService.selectAllAnnouncements(current,size,keyWord);
        if(Ipage == null)//判空处理
            return Result.fail(400,"未查询到公告信息",null);
        return Result.ok(200,"公告信息遍历成功",Ipage);
    }

    //添加单个公告
    @PostMapping("addAnnouncement")
    public Result addAnnouncement(@RequestBody Announcement announcement) {
        if (announcementService.addAnnouncement(announcement))
            return Result.ok(200,"添加公告成功",1);
        return Result.fail(400,"添加公告失败",null);
    }

    //根据id集合批量删除公告
    @DeleteMapping("delAnnouncementsByIdList")
    public Result delAnnouncementsByIdList(@RequestBody List<Integer> announcementIds){
        if (announcementService.delAnnouncementsByIdList(announcementIds))
            return Result.ok(200,"删除公告成功",1);
        return Result.fail(400,"删除公告失败",null);
    }
    //修改公告信息
    @PutMapping("updateAnnouncement")
    public Result updateAnnouncement(@RequestBody Announcement announcement){
        if (announcementService.updateAnnouncement(announcement))
            return Result.ok(200,"修改公告成功",1);
        return Result.fail(400,"修改公告失败",null);
    }

}

